<template>
	<view class="detail-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="代表风采"></uni-navbar></view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="form-item" required label="标题" prop="title">
					<u-input v-model="form.title" type="text" placeholder="请输入标题" />
				</u-form-item>

				<u-form-item class="form-item" required label="类型" prop="type">
					<u-input v-model="type" type="select" :select-open="typeShow" placeholder="请选择类型" @click="typeShow = true" />
					<u-select :list="typeList" v-model="typeShow" @confirm="typeConfirm"></u-select>
				</u-form-item>

				<u-form-item class="form-item" required label="发布时间" prop="pubDate">
					<date-time-picker placeholder="请选择发布时间" fields="day" @change="timeChange($event, 'pubDate')"></date-time-picker>
				</u-form-item>

				<u-form-item class="form-item" label="视频链接">
					<u-input v-model="form.videoLink" type="text" placeholder="请输入视频链接" />
				</u-form-item>
				<u-form-item class="form-item" label="其他链接">
					<u-input v-model="form.otherLink" type="text" placeholder="请输入其他链接" />
				</u-form-item>

				<u-form-item class="form-item" label="内容">
					<u-input v-model="form.content" type="textarea" :placeholder="'请输入内容'" :clearable="false" :height="160" :maxlength="-1" />
				</u-form-item>
				<u-form-item class="form-item" label="上传文件"><uni-file-upload style="width: 500rpx"></uni-file-upload></u-form-item>
			</u-form>
		</view>

		<view class="fiexd-btm"><u-button class="form-btn" type="error" @click="confirm">发布</u-button></view>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import uniFileUpload from '@/components/uni-file-uploader.vue';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker,
		uniFileUpload
	},
	data() {
		return {
			type: '',
			typeShow: false,
			typeList: [
				{
					label: '戏曲',
					value: '4'
				},
				{
					label: '绘画',
					value: '3'
				},
				{
					label: '美文',
					value: '6'
				},
				{
					label: '代表事迹',
					value: '7'
				},
				{
					label: '书法',
					value: '2'
				},
				{
					label: '摄影',
					value: '1'
				},
				{
					label: '音乐',
					value: '5'
				}
			],

			form: {
				title: '',
				pubDate: '',
				type: '',
				otherLink: '',
				videoLink: '',
				content: '',
				clientCode: '',
				createBy: ''
			},
			rules: {
				title: [
					{
						required: true,
						message: '请输入标题',
						trigger: 'change'
					}
				],
				type: [
					{
						required: true,
						message: '请选择类型',
						trigger: 'change'
					}
				],
				pubDate: [
					{
						required: true,
						message: '请选择发布时间',
						trigger: 'change'
					}
				]
			}
		};
	},
	onLoad() {
		this.loadLogin();
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	onUnload() {
		this.$store.commit('filePush', []);
	},
	computed: {
		...mapState(['userInfo', 'fileList'])
	},
	methods: {
		timeChange({ date, dt }, idx) {
			this.form[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		typeConfirm(e) {
			this.type = e[0].label;
			this.form.type = e[0].value;
		},
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
			} else {
				this.$logout();
			}
		},
		confirm() {
			this.$refs.uForm.validate((valid) => {
				let data = {};
				this.form.clientCode = this.clientCode;
				this.form.createBy = this.userId;

				data = {
					wyfc: this.form,
					fileList: this.fileList
				};
				this.$request('/wyfc/add', data, {
					load: true
				}).then((res) => {
					this.$msg(res.msg);
					setTimeout(() => {
						let pre = this.$prePage();
						if (pre && pre.loadData) {
							pre.loadData('refresh');
						}
						uni.navigateBack();
					}, 500);
				});
			});
		}
	}
};
</script>

<style lang="scss">
.detail-view {
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid $u-border-color;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
